package com.news.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.news.entity.News;
import com.news.entity.User;
import com.news.util.PageModel;

public class UserDaoImpl implements UserDao {

	private SessionFactory session;

	@Override
	public void setSession(SessionFactory session) {
		this.session = session;
	}

	// login
	@Override
	public User login(User user) {
		String hql = "from User u where u.username=? and u.password=? and u.isdel=?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setString(0, user.getUsername());
		query.setString(1, user.getPassword());
		query.setInteger(2, 0);
		return (User) query.uniqueResult();

	}

	// zhuce
	@Override
	public void addUser(User user) {
		session.getCurrentSession().save(user);
	}

	// getAllUser
	@Override
	public List<User> getAllUser() {
		String hql = "from User u where u.isdel=? ";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setInteger(0, 0);

		return query.list();
	}

	@Override
	public User getUser(int userid) {
		String hql = "from User u where u.id=?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setInteger(0, userid);

		return (User) query.uniqueResult();
	}

	// updateuser
	@Override
	public boolean updateUser(User user) {
		String hql = "update User u set u.username = ?,u.password=? where u.id = ?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setString(0, user.getUsername());
		query.setString(1, user.getPassword());
		query.setInteger(2, user.getUserid());

		return (query.executeUpdate() > 0);
	}

	// deluser
	@Override
	public boolean delUser(int userid) {

		String hql = "update User u set u.isdel=? where u.userid = ?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setInteger(0, 1);
		query.setInteger(1, userid);

		return (query.executeUpdate() > 0);
	}

	// 分页查询
	@Override
	public PageModel getAll(Integer currentPage, Integer rows) {
		Long totalsl = getAllCount();
		int totals = Integer.parseInt(totalsl.toString());
		List<News> datas = allUser(currentPage, rows);
		return new PageModel(totals, datas, rows, currentPage);
	}

	@Override
	public Long getAllCount() {
		return (Long) session.getCurrentSession()
				.createQuery("select count(*) from User where isdel=0")
				.uniqueResult();
	}

	@Override
	public List<News> allUser(int currPage, int pageSize) {
		StringBuffer sb = new StringBuffer();
		sb.append("from User as lo  where isdel=?");
		Query query = session.getCurrentSession().createQuery(sb.toString());
		query.setFirstResult((currPage - 1) * pageSize);
		query.setMaxResults(pageSize);
		query.setInteger(0, 0);
		return query.list();
	}

	// 模糊分页查询
	@Override
	public PageModel checkAll(String name, Integer currentPage, Integer rows) {
		Long totalsl = getAllCount();
		int totals = Integer.parseInt(totalsl.toString());
		List<News> datas = checkallUser(name, currentPage, rows);
		return new PageModel(totals, datas, rows, currentPage);
	}

	@Override
	public Long checkCount() {
		return (Long) session.getCurrentSession()
				.createQuery("select count(*) from User where isdel=0")
				.uniqueResult();
	}

	@Override
	public List<News> checkallUser(String name, int currPage, int pageSize) {
		StringBuffer sb = new StringBuffer();
		sb.append("from User as lo  where isdel=? and name like '%" + name
				+ "%'");
		Query query = session.getCurrentSession().createQuery(sb.toString());
		query.setFirstResult((currPage - 1) * pageSize);
		query.setMaxResults(pageSize);
		query.setInteger(0, 0);
		return query.list();
	}

	@Override
	public List<User> checkusername(String username) {
		String hql = "from User u where u.username = ?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setString(0, username);

		return query.list();
	}

}
